package controller;

import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;

import db.oaUser;
import db.sysuser;
import utils.IResult;
import utils.Logutils;
import utils.MD5Util;
import utils.loginuser;

public class LoginController extends Controller{
	public void index() {
		renderText("GWF Login");
	}
//	oa user login
	public void oaenter(@Para("appid")String appid,@Para("appsec")String appsec,@Para("username")String username,@Para("userpass")String userpass) {
//		System.out.println("enter01");
		if(!(appid.equals("xE0E8S8A") && appsec.equals("3X9sxrF987"))) {
//			System.out.println("loginfail1");
			renderJson(IResult.fail("Err No: L9071"));
			return;
		}
		oaUser itemuser = oaUser.dao.findFirst("select * from sys_oauser where loginid=?", username);
		if(itemuser != null){
//			System.out.println(username);
			//鍒ゆ柇鐢ㄦ埛鏄惁绂佺敤
			int iAccess = itemuser.getInt("state");
			if(iAccess < 1 || iAccess > 2) {
				renderJson(IResult.fail("Err No: L9082"));
				return;
			}
			loginuser cuser = new loginuser();
			
			String account = itemuser.getStr("loginid");
			String name = itemuser.getStr("lastname");
			int userId = itemuser.getInt("oaid");
			
			cuser.setAccount(account);
			cuser.setId(userId);
			cuser.setUserrole(iAccess);
			cuser.setName(name);
			cuser.setOauser(true);
			getSession().setAttribute("user",cuser);
			
			Logutils.addlog(name, "登入系统");
			
			//鍒朵綔json,杩斿洖鍓嶇  鍙繑鍥炲墠绔痶oken,鐢ㄦ埛鏄剧ず鍚�,鏉冮檺
//			System.out.println("loginOk");
			renderJson(IResult.ok(cuser));
			
			return;
		}
//		System.out.println("loginfail2");
		renderJson(IResult.fail("Err No: L9083"));
		return;
	}
	
	
//    退出系统记录
	public void oaexit() {
	    loginuser cuser = (loginuser) getSession().getAttribute("user");
	    if (cuser != null) {
	        String name = cuser.getName();
	        getSession().removeAttribute("user");
	        Logutils.addlog(name, "退出系统");
	        renderJson(IResult.ok());
	    } else {
	        renderJson(IResult.fail("未找到登录用户"));
	    }
	}
	
	
	//杩樻槸鍦ㄩ娆＄櫥褰曡姹傝緭鍏pp鐨勪袱涓瘑閽ワ紝閬垮厤鏈嶅姟鍣ㄨ鏀诲嚮
	public void enter(@Para("appid")String appid,@Para("appsec")String appsec,@Para("username")String username,@Para("userpass")String userpass) {
		if(!(appid.equals("xE0E8S8A") && appsec.equals("3X9sxrF987"))) {
			renderJson(IResult.fail("Err No: L9071"));
			return;
		}

		//缁х画鍒ゆ柇user淇℃伅鏄惁绗﹀悎瑕佹眰
		sysuser itemuser = sysuser.dao.findFirst("select * from sys_user where account=?", username);
		if(itemuser != null){
			//鍒ゆ柇鐢ㄦ埛鏄惁绂佺敤
			int iAccess = itemuser.getInt("state");
			if(iAccess < 1 || iAccess > 5) {
				renderJson(IResult.fail("Err No: L9081"));
				return;
			}

			//楠岃瘉pass
			String pass = itemuser.getStr("pass");
	        String veris = itemuser.getStr("passver");
	        
			String passmd5 = MD5Util.getMD5Code(userpass+veris);
			
			if(pass.equals(passmd5)) {
				loginuser cuser = new loginuser();
				
				String account = itemuser.getStr("account");
				String name = itemuser.getStr("name");
				int userId = itemuser.getInt("id");
				
				cuser.setAccount(account);
				cuser.setId(userId);
				cuser.setUserrole(iAccess);
				cuser.setName(name);
				cuser.setOauser(false);
				getSession().setAttribute("user",cuser);
				
				Logutils.addlog(name, "登入系统");
				
				//鍒朵綔json,杩斿洖鍓嶇  鍙繑鍥炲墠绔痶oken,鐢ㄦ埛鏄剧ず鍚�,鏉冮檺
				renderJson(IResult.ok(cuser));
				return;
			}else {
				renderJson(IResult.fail("Err No: L1070"));
				return;
			}
			
		}else {
			renderJson(IResult.fail("Err No: L1071"));
			return;
		}
			

	}
	public void logoff() {
		//鐧诲嚭   闇�瑕佹敼涓篢okenManager澶勭悊
		getSession().removeAttribute("user");
		renderJson(IResult.ok());
	}
//	获取当前登陆用户的信息
	public void isuserlogin() {
		loginuser user = (loginuser)getSession().getAttribute("user");
		if(user!=null)
		{
			renderJson(IResult.ok());
			return;
		}
		renderJson(IResult.fail(""));
	}
	
	//褰撳墠鐢ㄦ埛鑾峰彇鐢ㄦ埛淇℃伅杩斿洖鍓嶇
	public void getself() {
		loginuser user = (loginuser)getSession().getAttribute("user");
		if(user!=null)
		{
			renderJson(IResult.ok(user));
			return;
		}
		renderJson(IResult.fail("缃戠粶鑾峰彇鐢ㄦ埛淇℃伅澶辫触銆�"));

	}
	//杩涜褰撳墠鐢ㄦ埛淇℃伅缂栬緫
	public void editself() { // 修改密码调用此接口
		//鍙兘淇敼鑷繁鐨勫瘑鐮併�佸崟浣嶃�佺數璇濄�侀偖绠�
		String pass = getPara("key");
		if(pass == null) {
			renderJson(IResult.fail("淇敼鐢ㄦ埛淇℃伅澶辫触銆�"));
			return;
		}
		loginuser user = (loginuser)getSession().getAttribute("user");
		if(user!=null && !user.isOauser()) {
			sysuser dbuser = sysuser.dao.findById(user.getId());
			String passver = dbuser.getStr("passver");
	        String pwd = MD5Util.getMD5Code(pass + passver);
	        dbuser.set("pass", pwd).update();
			renderJson(IResult.ok());
			return;
		}
		renderJson(IResult.fail("淇敼鐢ㄦ埛淇℃伅澶辫触銆�"));
	}
}
